package com.castle.fortress.admin.orders.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.orders.entity.GoodsOrderItemEntity;
import com.castle.fortress.admin.orders.dto.GoodsOrderItemDto;
import com.castle.fortress.admin.orders.mapper.GoodsOrderItemMapper;
import com.castle.fortress.admin.orders.service.GoodsOrderItemService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 订单详情 服务实现类
 *
 * @author Mgg
 * @since 2023-01-13
 */
@Service
public class GoodsOrderItemServiceImpl extends ServiceImpl<GoodsOrderItemMapper, GoodsOrderItemEntity> implements GoodsOrderItemService {
    /**
    * 获取查询条件
    * @param goodsOrderItemDto
    * @return
    */
    public QueryWrapper<GoodsOrderItemEntity> getWrapper(GoodsOrderItemDto goodsOrderItemDto){
        QueryWrapper<GoodsOrderItemEntity> wrapper= new QueryWrapper();
        if(goodsOrderItemDto != null){
            GoodsOrderItemEntity goodsOrderItemEntity = ConvertUtil.transformObj(goodsOrderItemDto,GoodsOrderItemEntity.class);
            wrapper.eq(goodsOrderItemEntity.getOrderNumber() != null,"order_number",goodsOrderItemEntity.getOrderNumber());
            wrapper.eq(goodsOrderItemEntity.getGoodsId() != null,"goods_id",goodsOrderItemEntity.getGoodsId());
            wrapper.like(StrUtil.isNotEmpty(goodsOrderItemEntity.getGoodsName()),"goods_name",goodsOrderItemEntity.getGoodsName());
            wrapper.like(StrUtil.isNotEmpty(goodsOrderItemEntity.getSpecName()),"spec_name",goodsOrderItemEntity.getSpecName());
            wrapper.eq(goodsOrderItemEntity.getOrderStatus() != null,"order_status",goodsOrderItemEntity.getOrderStatus());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<GoodsOrderItemDto> pageGoodsOrderItem(Page<GoodsOrderItemDto> page, GoodsOrderItemDto goodsOrderItemDto) {
        QueryWrapper<GoodsOrderItemEntity> wrapper = getWrapper(goodsOrderItemDto);
        Page<GoodsOrderItemEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<GoodsOrderItemEntity> goodsOrderItemPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<GoodsOrderItemDto> pageDto = new Page(goodsOrderItemPage.getCurrent(), goodsOrderItemPage.getSize(),goodsOrderItemPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(goodsOrderItemPage.getRecords(),GoodsOrderItemDto.class));
        return pageDto;
    }

    @Override
    public IPage<GoodsOrderItemDto> pageGoodsOrderItemExtends(Page<GoodsOrderItemDto> page, GoodsOrderItemDto goodsOrderItemDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        GoodsOrderItemEntity goodsOrderItemEntity = ConvertUtil.transformObj(goodsOrderItemDto,GoodsOrderItemEntity.class);
        List<GoodsOrderItemEntity> goodsOrderItemList=baseMapper.extendsList(pageMap,goodsOrderItemEntity);
        Long total = baseMapper.extendsCount(goodsOrderItemEntity);
        Page<GoodsOrderItemDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(goodsOrderItemList,GoodsOrderItemDto.class));
        return pageDto;
    }
    @Override
    public GoodsOrderItemDto getByIdExtends(Long id){
        GoodsOrderItemEntity  goodsOrderItemEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(goodsOrderItemEntity,GoodsOrderItemDto.class);
    }

    @Override
    public List<GoodsOrderItemDto> listGoodsOrderItem(GoodsOrderItemDto goodsOrderItemDto){
        QueryWrapper<GoodsOrderItemEntity> wrapper = getWrapper(goodsOrderItemDto);
        List<GoodsOrderItemEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,GoodsOrderItemDto.class);
    }
}

